package mymvc.observer
{
	public class ObserverManager
	{
		private var observerMap:Array = null;
		private static var instance:ObserverManager = null;
		
		public function ObserverManager()
		{
			observerMap = new Array();
		}
		
		public static function getInstance():ObserverManager{
			if(instance == null){
				instance = new ObserverManager();
			}
			return instance;
		}
		
		public function addObserver(observerName:String,
				target:Object,func:Function):void{
			var obj:Object = new Object();
			obj.observerName = observerName;
			obj.target = target;
			obj.func   = func  ;
			
			observerMap.push(obj);
		}
		
		public function execute(observerName:String,data:ObserverData):void{
			var targetObj:Object = null;
			for(var i:int = 0;i<observerMap.length;i++){
				if(observerName == observerMap[i].observerName){
					targetObj = observerMap[i];
					var func:Function = targetObj.func as Function;
					var arr:Array = new Array();
					arr.push(data);
					func.apply(targetObj.target,arr);
					break;
				}
			}
			if(targetObj == null){
				throw new Error("该观察者没找到!");
			}
		}
	}
}